//
//  HMEmoticonCollectionViewCell.swift
//  VVeibo21
//
//  Created by Apple on 16/11/2.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
//  表情视图 cell
class HMEmoticonCollectionViewCell: UICollectionViewCell {
    
    var emoticonArray: [HMEmoticon]? {
        didSet {
            //  设置数据的时候绑定表情按钮的数据
            guard let etnArray = emoticonArray else {
                return
            }
            
            //  先把表情按钮全部隐藏,在绑定数据的时候在设置显示
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            
            
            for (i, emoticon) in etnArray.enumerated() {
                //  根据下标获取对应的表情按钮
                let emoticonButton = emoticonButtonArray[i]
                //  绑定表情模型
                emoticonButton.emoticon = emoticon
                //  绑定数据的时候让其显示
                emoticonButton.isHidden = false
                if emoticon.type == "0" {
                    //  表示图片
                    emoticonButton.setImage(UIImage(named: emoticon.fullPath!), for: .normal)
                    emoticonButton.setTitle(nil, for: .normal)
                } else {
                    //  表示 emoji
                    emoticonButton.setTitle((emoticon.code! as NSString).emoji(), for: .normal)
                    emoticonButton.setImage(nil, for: .normal)
                }
                
            
            }
            
            
            
            
        }
    }
    
    //  设置当前的 cell 的 indexPath
    var indexPath: IndexPath? {
        didSet {
            guard let iph = indexPath else {
                return
            }
            messageLabel.text = "当前显示的第\(iph.section + 1)组第\(iph.item + 1)页"
        }
    }
    
    //  记录20表情按钮
    fileprivate lazy var emoticonButtonArray: [HMEmotiocnButton] = [HMEmotiocnButton]()
    
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.green, fontSize: 30, text: "")
        return label
        
    }()
    //  删除表情按钮
    fileprivate lazy var deleteEmoticonButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteEmoticonButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
    
        addChildButton()
        
        contentView.addSubview(deleteEmoticonButton)
//        contentView.addSubview(messageLabel)
//        
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        }
        
    
    }
    
    //  添加表情按钮
    private func addChildButton() {
        for _ in 0..<20 {
            let button = HMEmotiocnButton()
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            
            contentView.addSubview(button)
            emoticonButtonArray.append(button)
        }
    
    }
    
    // MARK: - 点击删除表情按钮逻辑处理
    @objc private func deleteEmoticonButtonAction() {
        //  执行删除表情内容的操作
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmotionButtonNotification), object: nil)
    
    }
    
    // MARK: - 点击表情事件处理
    @objc private func emoticonButtonAction(btn: HMEmotiocnButton) {
    
        //  获取按钮对应的表情模型
        let emoticon = btn.emoticon!
       
        //  发送点击表情按钮的通知
        NotificationCenter.default.post(name: NSNotification.Name( DidSelectedEmoticonButtonNotification), object: emoticon)
        
    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //  计算表情按钮的宽度
        let buttonWidth = width / 7
        //  计算表情按钮的高度
        let buttonHeight =  height / 3
        
        //  设置每个表情的 frame
        
        for (i, button) in emoticonButtonArray.enumerated() {
            //  计算列的索引
            let colIndex = i % 7
            //  计算行的索引
            let rowIndex = i / 7
            
            //  设置x,y坐标
            button.x = CGFloat(colIndex) * buttonWidth
            button.y = CGFloat(rowIndex) * buttonHeight
            
            
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
        }
        
        //  设置删除按钮的大小
        deleteEmoticonButton.size = CGSize(width: buttonWidth, height: buttonHeight)
        //  设置 x,y 坐标
        deleteEmoticonButton.x = width - buttonWidth
        deleteEmoticonButton.y = height - buttonHeight
        
    }
    
    
    
    
}
